###################################
#Figure 6c
###################################


library(foreign)
library(lattice)
library(Hmisc)
library(readstata13)
library(latticeExtra)

data<-read.dta13("Figure6c1_Preds_reshaped.dta")
data2<-read.dta13("Figure6c2_Preds_reshaped.dta")
days<-data[,1]
CycleLowBound1<-data[,2]
CycleUpBound1<-data[,3]
CycleLowBound32<-data[,4]
CycleUpBound32<-data[,5]
PostLowBound1<-data2[,2]
PostUpBound1<-data2[,3]
PostLowBound32<-data2[,4]
PostUpBound32<-data2[,5]

group<-c("Linear Cycle Trend", "Post-1994 Election Indicator")

x<-1:735

pdf(file="Figure6c.pdf", family="Bookman")
setTrellis()
xyplot(CycleLowBound1 ~ days | group, type="l", xlab='Days to Next Election', ylab='Probability of Hyper Divisive Vote', title="Hyper Divisive", xlim=rev(range(x)), ylim=c(0, .8), col=c("white"), key=list(text=list(c("Early in Time Series", "Late In Time Series")), points=list(pch=c(15, 15), col=c("gray35", "gray70")), columns=1))
main="Hyper Divisive Partisan Votes"

a<-c(days, rev(days))
b<-c(CycleLowBound1, rev(CycleUpBound1))
c<-c(CycleLowBound32, rev(CycleUpBound32))
d<-c(PostLowBound1, rev(PostUpBound1))
e<-c(PostLowBound32, rev(PostUpBound32))

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.polygon", list(x=a, y=b, col="gray35", border=NA))
trellis.unfocus( )

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.polygon", list(x=a, y=c, col="gray70", border=NA))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.polygon", list(x=a, y=d, col="gray35", border=NA))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.polygon", list(x=a, y=e, col="gray70", border=NA))
trellis.unfocus( )

dev.off()


##########################################
#Figure 6d
##########################################

library(foreign)
library(lattice)
library(Hmisc)
library(readstata13)
library(latticeExtra)

data<-read.dta13("Figure6d1_Preds.dta")
data2<-read.dta13("Figure6d2_Preds.dta")

days<-c(0,1)
Means<-c(data[3,4], data[4,4])

group<-c("Linear Cycle Trend", "Post-1994 Election Indicator")

pdf(file="Figure6d.pdf", family="Bookman")
setTrellis()
xyplot(Means ~ days | group, xlab='Session', ylab='Probability of Hyper Divisive Vote', xlim=c(-.5, 1.5), scales=list(x=list(at=c(0,1), labels=c("First", "Second"))), ylim=c(0, .8), col=c("white"), key=list(text=list(c("Early in Time Series", "Late In Time Series")), points=list(pch=c(15, 15), col=c("gray35", "gray70")), columns=1))

panel.axis(side="bottom", labels=FALSE)

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.segments", list(0, data[1,2], 0, data[1,4], col="gray35"))
trellis.unfocus( )

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.segments", list(1, data[2,2], 1, data[2,4], col="gray35"))
trellis.unfocus( )

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.points", list(x=0, y=data[1,3], col="gray35", pch=16))
trellis.unfocus( )

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.points", list(x=1, y=data[2,3], col="gray35", pch=16))
trellis.unfocus( )

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.segments", list(0, data[1,3], 1, data[2,3], col="gray35", lty=2))
trellis.unfocus( )

#

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.segments", list(0, data[3,2], 0, data[3,4], col="gray70"))
trellis.unfocus( )

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.segments", list(1, data[4,2], 1, data[4,4], col="gray70"))
trellis.unfocus( )

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.points", list(x=0, y=data[3,3], col="gray70", pch=16))
trellis.unfocus( )

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.points", list(x=1, y=data[4,3], col="gray70", pch=16))
trellis.unfocus( )

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.segments", list(0, data[3,3], 1, data[4,3], col="gray70", lty=1))
trellis.unfocus( )

#

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.segments", list(0, data2[1,2], 0, data2[1,4], col="gray35"))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.segments", list(1, data2[2,2], 1, data2[2,4], col="gray35"))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.points", list(x=0, y=data2[1,3], col="gray35", pch=16))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.points", list(x=1, y=data2[2,3], col="gray35", pch=16))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.segments", list(0, data2[1,3], 1, data2[2,3], col="gray35", lty=2))
trellis.unfocus( )

#

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.segments", list(0, data2[3,2], 0, data2[3,4], col="gray70"))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.segments", list(1, data2[4,2], 1, data2[4,4], col="gray70"))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.points", list(x=0, y=data2[3,3], col="gray70", pch=16))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.points", list(x=1, y=data2[4,3], col="gray70", pch=16))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.segments", list(0, data2[3,3], 1, data2[4,3], col="gray70", lty=2))
trellis.unfocus( )

#

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.segments", list(-.4, .77, -.3, .77, col="black", lty=2))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.text", list(x=.5, y=.77, labels="Statistically significant difference", cex=.8))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.segments", list(-.45, .79, 1.45, .79, col="black", lty=1))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.segments", list(-.45, .75, 1.45, .75, col="black", lty=1))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.segments", list(-.45, .79, -.45, .75, col="black", lty=1))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.segments", list(1.45, .79, 1.45, .75, col="black", lty=1))
trellis.unfocus( )

dev.off()
